package com.example.administrator.activity.parcitise.Storage.Inner_output;

import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.activity.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class FileOuputActivity extends AppCompatActivity {
    Button file_outer_read,getFile_outer_writer;
    TextView file_outer_tv;

    ActivityCompat.OnRequestPermissionsResultCallback resultCallback=new ActivityCompat.OnRequestPermissionsResultCallback() {
        @Override
        public void onRequestPermissionsResult(int i, @NonNull String[] strings, @NonNull int[] ints) {
           FileOuputActivity.this.onRequestPermissionsResult(i,strings,ints);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_ouput);
        init();
        String[] p={"android.permission.WRITE_EXTERNAL_STORAGE"};
        ActivityCompat.requestPermissions(FileOuputActivity.this,p,1);
        View.OnClickListener clickListener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            switch (v.getId()){
                case R.id.ch3_file_outer_write:
                    write();
                    break;
                case R.id.ch3_file_outer_read:
                    read();
                    break;
            }
            }
        };
        getFile_outer_writer.setOnClickListener(clickListener);
        file_outer_read.setOnClickListener(clickListener);
        file_outer_tv.setOnClickListener(clickListener);
    }

    /* 1、动态申请权限
      2、判断外部设备是否可用
        *     若可用：
        *    1）获取SD卡路径
        *    2）生成File对象
        *    3）利用Java流将内容写入文件
        *       FileOutputStream的构造方法有传入File参数
        *       将其转换为BufferedWriter
        *       */
    private void read() {
        String device=Environment.getExternalStorageState();
        if (device.equals(Environment.MEDIA_MOUNTED)){
            File sdPath=Environment.getExternalStorageDirectory();
            File file=new File(sdPath,"h2.txt");
            FileInputStream inputStream;
            BufferedReader reader;
            try {
                inputStream=new FileInputStream(file);
                reader=new BufferedReader(new InputStreamReader(inputStream));
                String line, s="";
                while ((line=reader.readLine())!=null){
                    s+=line+"\n";
                }
                file_outer_tv.setText(s);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * 1.动态申请权限
     * 2.判断外部设备是否可用
     *     若可用
     *     1）获取SD卡路径
     *     2）生成File对象
     *     3）利用Java流将内容写入文件
     *     FileOutputStream的构造方法有传入File参数
     *     将其转换为BufferedWriter
     */
    private void write() {

        String message="Android\nIOS";
        /**
         * 获取外部设备的状态
         */
        String device=Environment.getExternalStorageState();
        /**
         * MEDIA_MOUNTED 存储媒体已经挂载，并且挂载点可读\写
         */
        if (device.equals(Environment.MEDIA_MOUNTED)){
            File sdPath=Environment.getExternalStorageDirectory();
            /**
             * file存储的文件
             */
            File file=new File(sdPath,"h2.txt");
            /**
             * 通过IO流的方式将 message中的内容 写入到file文件
             */
            FileOutputStream outputStream;
            BufferedWriter writer;
            try {
                outputStream=new FileOutputStream(file);
                writer=new BufferedWriter(new OutputStreamWriter(outputStream));
                writer.write(message);
                writer.flush();
                Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    private void init() {
        file_outer_read=findViewById(R.id.ch3_file_outer_read);
        getFile_outer_writer=findViewById(R.id.ch3_file_outer_write);
        file_outer_tv=findViewById(R.id.ch3_file_outer_tv);
    }
}
